<?php

namespace App\Http\Controllers\Innerend;


use App\Models\User;
use App\Models\UserPay;

class UserPayController extends BaseController
{

    // 支付记录
    public function initialize()
    {
        parent::initialize();

        $this->model = 'UserPay';
    }


    // 列表数据展示
    protected function listTable($lists)
    {
        // 整理
        $trList = [];
        if(empty($lists)){
            return $trList;
        }

        // 获取用户信息
        $uids  = array_column($lists, 'uid');
        $users = (new User())->isCache(true)->getListInIds($uids);

        // 展示列表表格
        $listHtml = $this->adminList();
        $model    = new UserPay();
        $tyeps    = $model->getType(false);
        $statuss  = $model->getSatus(false);
        foreach($lists as $key => $item){
            // 数据
            $_id      = $item['id'];
            $status   = $item['status'] == 1 ? '正常' : '禁用';
            $statusOp = $item['status'] == 1 ? '禁用' : '启用';
            // 操作
            $operate = '';
            //$operate .= $listHtml->operate_status(['id' => $_id, 'value' => 1 - $item['status']], $statusOp, $item['status']);
            $operate .= $listHtml->operate_edit(['id' => $_id], '编辑');
            $operate .= $listHtml->operate_soft_delete(['id' => $_id], '你确定要删除该用户吗？');

            // 页面
            $tr = $listHtml->list_check($item['id']);
            $tr .= $listHtml->list_td($users[$item['uid']]['nickname'] ?? '-');
            $tr .= $listHtml->list_td($item['auth_id']);
            $tr .= $listHtml->list_td($item['order_id']);
            $tr .= $listHtml->list_td($item['trade_no']);
            $tr .= $listHtml->list_td($item['money']);
            $tr .= $listHtml->list_td($tyeps[$item['type']] ?? '-');
            $tr .= $listHtml->list_td($statuss[$item['status']] ?? '-');
            $tr .= $listHtml->list_td(date('Y-m-d H:i', $item['ctime']));
            //$tr .= $listHtml->list_td($operate);

            // 整合
            $trList[] = $listHtml->list_tr($tr);
        }

        // 整合
        return $trList;
    }

    // 列表顶部按钮
    protected function listTopButton()
    {
        $addUrl     = Url('/innerend/user-pay/add');
        $top_button = '<a href="javascript:;" onclick="onEdit(\''.$addUrl.'\')" class="btn btn-success radius">添加支付</a> ';
        $this->assign('top_button', $top_button);
        $this->assign('top_button', '');
    }

    // 列表字段
    protected function list_fields()
    {
        //  'operate' => '操作',
        return ['uid' => '用户ID', 'auth_id' => '认证ID', 'order_id' => '订单ID', 'trade_no' => '订单号', 'money' => '金额', 'type' => '操作类型', 'status' => '状态', 'ctime' => '创建时间',];
    }

    // 列表搜索
    protected function list_search()
    {
        $model   = new UserPay();
        $tyeps   = $model->getType(false);
        $statuss = $model->getSatus(false);
        $request = $this->optLogic()->getData();
        $form    = $this->adminList();
        $uid     = $form->formInput('uid', $request['uid'] ?? null, '用户id', '120px');

        $status     = $form->formSelect('status', $request['status'] ?? null, $statuss);
        $trade_no   = $form->formInput('trade_no', $request['trade_no'] ?? null, '订单号', '120px');
        $start_time = $form->formDateTime('start_time', isset($request['start_time']) ? date("Y-m-d H:i:s", $request['start_time']) : null, '开始时间', 'datetime');
        $end_time   = $form->formDateTime('end_time', isset($request['end_time']) ? date("Y-m-d H:i:s", $request['start_time']) : null, '结束时间时间', 'datetime');
        $search     = ['status' => $status, 'uid' => $uid, 'trade_no' => $trade_no, 'start_time' => $start_time, 'end_time' => $end_time];
        $this->assign('search', $search);

        $searchHide = ['channel'];
        $this->assign('searchHide', []);
    }

    // 表单页面
    protected function formTable()
    {
        $manage = $this->checkManage($this->model);
        if($manage === false){
            return $this->error($manage->getError());
        }

        $html = '';
        $info = $manage->onShow();
        $form = $this->adminForm();

        $model   = new UserPay();
        $tyeps   = $model->getType(false);
        $statuss = $model->getSatus(false);
        // 用户
        $uInfo = (new User())->isCache(true)->getOneById($info['uid'] ?? 0);

        // 用户名
        //$uid         = $form->ajaxSearch('uid', $info['uid'] ?? '', 'User');
        $uid         = $form->ajaxSingleFromList('uid', $uInfo, 'User', 'id', 'username');
        $status      = $form->formRadio('status', $info['status'] ?? 1, $statuss);
        $auth_id     = $form->formInput('auth_id', $info['auth_id'] ?? '', '认证ID');
        $order_id    = $form->formInput('order_id', $info['order_id'] ?? '', '订单ID');
        $trade_no    = $form->formInput('trade_no', $info['trade_no'] ?? '', '三方平台订单号');
        $money       = $form->formInput('money', $info['money'] ?? '', '金额');
        $description = $form->formTextarea('description', $info['description'] ?? '');
        $type        = $form->formRadio('type', $info['type'] ?? 1, $tyeps);
        $_id         = $form->formHidden('id', $info['id'] ?? '');

        //$html     .= $form->makeItem('确认密码', $password, '添加时候不填默认密码123456，修改不填默认不修改');
        $html .= $form->makeItem('用户', $uid);
        $html .= $form->makeItem('认证ID', $auth_id);
        $html .= $form->makeItem('订单ID', $order_id);
        $html .= $form->makeItem('三方平台订单号', $trade_no);
        $html .= $form->makeItem('金额', $money);
        $html .= $form->makeItem('支付类型', $type);
        $html .= $form->makeItem('支付状态', $status);
        $html .= $form->makeItem('描述', $description);
        $html .= $_id;

        return $html;
    }


}


